package ca.etsmtl.log430.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class ResourceChecker {

	static int PRIORITY_H = 100;
	static int PRIORITY_M = 50;
	static int PRIORITY_L = 25;
	
	public ResourceChecker(){
		
	}
	
	public boolean validate(Resource resource, ProjectList inputProjectList, Project newProject){
		
		boolean ok = true;
		ArrayList<Project> projectList = new ArrayList<Project>();
		
		//get all projects
		projectList = getAllProjects(resource, inputProjectList);
		projectList.add(newProject);
	
		//find earliest and latest project start date 
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date latestDate = null;
		Date earliestDate = null;
		
		try {
			latestDate = formatter.parse(projectList.get(0).getEndDate());
			earliestDate = formatter.parse(projectList.get(0).getStartDate());
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		
		for(int i =1; i < projectList.size(); i++){
			Date start = null;
			Date end = null;
			if(projectList.size() > 1){
				try {
					start = formatter.parse(projectList.get(i).getStartDate());
					end = formatter.parse(projectList.get(i).getEndDate());
				} catch (ParseException e) {
					e.printStackTrace();
				}
				
				if(start.before(earliestDate))
					earliestDate = start;
				
				if(end.after(latestDate))
					latestDate = end;
			}
		}
					
		//iterate every day
		
		Calendar start = Calendar.getInstance();
		start.setTime(earliestDate);
		Calendar end = Calendar.getInstance();
		end.setTime(latestDate);
	
		for (Date date = start.getTime(); !start.after(end); start.add(Calendar.DATE, 1), date = start.getTime()) {
			int assigned = 0;
			//iterate through projects  
			for(int i =0; i < projectList.size(); i++){
				//check if in range
				Date projectStartDate = null;
				Date projectEndDate = null;
				try {
					projectEndDate = formatter.parse(projectList.get(i).getEndDate());
					projectStartDate = formatter.parse(projectList.get(i).getStartDate());
				} catch (ParseException e) {
					e.printStackTrace();
				}
				if((date.equals(projectStartDate) || date.after(projectStartDate)) && 
						(date.equals(projectEndDate) || date.before(projectEndDate))){
					//get project % and sum to total
					assigned += getProjectAssignmentLevel(projectList.get(i));
				}
			}
			
			//check if overasigned
			if(assigned > 100){
				ok = false;
				System.out.println();
				System.out.println("Error: Resource " + resource.getID() + " is over asigned (" + assigned 
						+ "%) on date " + date.toString() + "!");
				System.out.print("Projects assigned: ");
				for(int i =0; i < projectList.size(); i++){
					System.out.print(projectList.get(i).getProjectName() + " ");
				}
				System.out.println();
			}
		}
						
				
		return ok;
	}
	
	private int getProjectAssignmentLevel(Project p){
		if(p.getPriority().equals("H"))
			return PRIORITY_H;
		else if (p.getPriority().equals("M"))
			return PRIORITY_M;
		else if (p.getPriority().equals("L"))
			return PRIORITY_L;
		return -1;
	}
	
	private ArrayList<Project> getAllProjects(Resource resource, ProjectList inputProjectList){
		
		//currently assigned
		ArrayList<Project> list = new ArrayList<Project>();
		
		boolean done;
		Project project;

		resource.getProjectsAssigned().goToFrontOfList();
		done = false;
		while (!done) {
			project = resource.getProjectsAssigned().getNextProject();

			if (project == null) {
				done = true;
			} else {
				list.add(inputProjectList.findProjectByID(project.getID()));

			} // if
		} // while	

		//previously assigned
		resource.getPreviouslyAssignedProjectList().goToFrontOfList();
		done = false;
		while (!done) {
			project = resource.getPreviouslyAssignedProjectList().getNextProject();
			if (project == null || project.getID().equals("")) {
				done = true;
			} else {
				list.add(inputProjectList.findProjectByID(project.getID()));
			} // if

		} // while
		return list;
		
	}
}
